package sy.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.OrderStay;
import sy.service.OrderStayServiceI;

@Controller
@RequestMapping("/orderStayController")
public class OrderStayController extends BaseController {
	
	@Autowired
	private OrderStayServiceI orderStayService;
	
	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(OrderStay order) throws Exception {
		return orderStayService.datagrid(order);
	}
	
	@RequestMapping("/datagrid/accepted")
	@ResponseBody
	public DataGrid datagridForAccepted(OrderStay order) throws Exception {
		return orderStayService.datagridForAccepted(order);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(OrderStay order, HttpSession session) {
		Json j = new Json();
		try {
			OrderStay result = orderStayService.add(order);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(result);
			
			//add log
			logUserAction(session, "/orderStayController/add", result.toString());
			
		} catch (Exception e) {
			j.setMsg("添加失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(OrderStay order, HttpSession session) {
		Json j = new Json();
		try {
			OrderStay result = orderStayService.saveStay(order);
			j.setSuccess(true);
			j.setMsg("保存成功！");
			logUserAction(session, "/orderStayController/edit", result.toString());
		} catch (Exception e) {
			j.setMsg("保存失败：" + e.getMessage());
		}
		return j;
	}

	@RequestMapping("/save_or_submit")
	@ResponseBody
	public Json edit_ok(OrderStay order, HttpSession session) {
		Json j = new Json();
		try {
			OrderStay result = orderStayService.edit(order);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			logUserAction(session, "/orderStayController/save_or_submit", result.toString());
		} catch (Exception e) {
			j.setMsg("修改失败：" + e.getMessage());
		}
		return j;
	}

	@RequestMapping("/cancel")
	@ResponseBody
	public Json cancel(OrderStay order, HttpSession session) {
		Json j = new Json();
		try {
			OrderStay result = orderStayService.editStayStatus(order);
			j.setSuccess(true);
			j.setMsg("取消成功！");
			logUserAction(session, "/orderStayController/cancel", result.toString());
		} catch (Exception e) {
			j.setMsg("取消失败：" + e.getMessage());
		}
		return j;
	}

	@RequestMapping("/hotels")
	@ResponseBody
	public Json getHotelsByType(String stayType) {
		System.out.println(stayType);
		Json j = new Json();
		try {
			Map<String, String> result = orderStayService.getHotelsByType(stayType);
			j.setSuccess(true);
			j.setObj(result);		
		} catch (Exception e) {
			j.setMsg("根据订单类型得到单位列表失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/roomNames")
	@ResponseBody
	public Json getRoomNamesByHotelId(String hotelId) {
		System.out.println(hotelId);
		Json j = new Json();
		try {
			Map<String, String> result = orderStayService.getRoomNamesByHotelId(hotelId);
			j.setSuccess(true);
			j.setObj(result);		
		} catch (Exception e) {
			j.setMsg("根据单位得到房型列表失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/addDetails")
	@ResponseBody
	public Json addDetails(OrderStay order, HttpSession session) {
		Json j = new Json();
		try {
			OrderStay result = orderStayService.addDetails(order);
			j.setSuccess(true);
			j.setMsg("生成详单成功！");
			//j.setObj(result);
			
			//add log
			logUserAction(session, "/orderStayController/addDetails", result.toString());
			
		} catch (Exception e) {
			j.setMsg("生成详单失败：" + e.getMessage());
		}
		return j;
	}

}
